'use strict';
//获取数据库对象
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event);
	const{
		password, 
		user_name,
		author_name, 
		avatar, 
		gender,
		article_ids=[],
		article_likes_ids=[],
		author_likes_ids=[],
		explain="",
		fans_count=0,
		follow_count= 0,
		integral_count= "",
		label_ids=[],
		professional= "",
		user_status= "normal",
		thumbs_up_article_ids= [],
		}=event;
	console.log("11 user_name:",user_name,"11 password:",password);
	//获取user表
	const userInfo = db.collection("user");
	const user = await userInfo.where({
		user_name:user_name,
		}).get();
		console.log("user:",user)
		
	let status = 0;
	let msg='';
	// 判断
	if(user.data.length != 0 ){
		//用户名已经存在
		status = 1;
		msg = "用户名已经存在";
	}else if(password == ''|| user_name == ''){
		//输入信息不全
		status = 2;
		msg = "输入信息不全";
	}else if(gender == '' || password == ''|| user_name == ''){
		//选择性别
		status = 4;
		msg = "请准确输入";
	}else{
		//存入信息
		
		const res = await userInfo.add({
			"password":password, 
			"user_name":user_name,
			"author_name":author_name,
			"avatar":avatar,
			"gender":gender,
			"article_ids": article_ids,
			"article_likes_ids": article_likes_ids,
			"author_likes_ids": author_likes_ids,
			"explain": explain,
			"fans_count": fans_count,
			"follow_count": follow_count,
			"integral_count": integral_count,
			"label_ids":label_ids,
			"professional": professional,
			"user_status": user_status,
			"thumbs_up_article_ids": thumbs_up_article_ids
			})
		status = 0;
		msg = "注册成功";
	}
	
	// const res =  userInfo.add({"password":password, "user_name":user_name})
	//返回数据给客户端
	return {
		"status":status,
		"msg":msg
	}
};
